﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace Frontend
{
    class BorderedGrid: Grid
    {
        public static readonly DependencyProperty BorderPenProperty;

        static BorderedGrid()
        {
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.AffectsMeasure = true;
            metadata.DefaultValue = new Pen(Brushes.Black, 1);

            BorderPenProperty = DependencyProperty.Register("BorderPen", typeof(Pen), typeof(BorderedGrid), metadata);
        }

        public Pen BorderPen 
        {
            get
            {
                return (Pen)GetValue(BorderPenProperty);
            }
            set
            {
                SetValue(BorderPenProperty, value);
            }
        }

        protected override void OnRender(System.Windows.Media.DrawingContext dc)
        {
            Pen line = new Pen(Brushes.Black, 1);

            base.OnRender(dc);

            dc.DrawRectangle(null, line, new Rect(0, 0, this.ActualWidth, this.ActualHeight));
            double height = 0;
            foreach (var r in this.RowDefinitions)
            {
                height += r.ActualHeight;
                dc.DrawLine(line, new Point(0, height), new Point(this.ActualWidth, height));
            }
            double width = 0;
            foreach (var c in this.ColumnDefinitions)
            {
                width += c.ActualWidth;
                dc.DrawLine(line, new Point(width, this.ActualHeight), new Point(width, 0));
            }
        }

        protected override Size MeasureOverride(Size constraint)
        {
            constraint.Width -= BorderPen.Thickness / 2;


            Size size = base.MeasureOverride(constraint);

            return size;
        }

        
    }
}
